package com.controller;
import com.entity.*;
import com.service.*;
import com.utils.AnnotationUtil;
import com.utils.DateUtil;
import com.utils.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;

@Controller
public class SystemMangerController
{
    @Resource
    private AsRoleService asRoleService;

    @Resource
    private AsFuntionService asFuntionService;

    @Resource
    AsRolePremissionService asRolePremissionService;

    @Resource
    private AsKeywordsService asKeywordsService;

    @Resource
    private AsAccountDetailService asAccountDetailService;

    @Resource
    private AsUserService asUserService;

    @Resource
    private AsLogsService asLogsService;

    //保存当前的用户角色id
    private int saveRoleId;

    //前往角色权限管理页面
    @RequestMapping(value = "/toRoleFuncManagePage")
    public String toRoleFuncManagePage( Model model )
    {
        //查询所有可用角色放入model中
        ArrayList<AsRole> allRoleList = asRoleService.returnAllAvailableRole();
        model.addAttribute( "allRoleList" , allRoleList );
        return "systemManage/roleFunction";
    }

    //响应角色权限配置ajax请求
    @RequestMapping(value = "/roleFuncManage" , produces="application/json;charset=UTF-8" )
    @ResponseBody
    @MethodLog( operateInfo = "查看角色权限配置" )
    public String showRoleFuncManage( Integer id , Integer roleId )
    {
        //System.out.println( "roleId:" + roleId );
        //System.out.println( id );
        ArrayList<FunctionPremissionTreeGrid> list = new ArrayList<>();
        //用户点击treegrid中根目录进入if 否则进入else
        if( id == null )
        {
            list = asFuntionService.returnAllRootTreeGrid( 0 );
            saveRoleId = roleId;
        }
        else if( ( saveRoleId != roleId ) )
        {
            list = asFuntionService.returnAllRootTreeGrid( 0 );
            saveRoleId = roleId;
        }
        else
        {
            list = asFuntionService.returnSubFunctionTreeGridByRoleIdAndParentId( id , roleId );
        }
        String jsonStr = JsonUtil.toJsonStrWithDateTime( list );

        return jsonStr;
    }

    //修改角色授权
    @RequestMapping(value = "/addOrUpdatePremission" , produces="application/json;charset=UTF-8")
    @ResponseBody
    @MethodLog( operateInfo = "修改角色授权" )
    public String addOrUpdatePremission( Integer roleId , Integer functionId , HttpSession session )
    {
        AsUser user = (AsUser) session.getAttribute( "user" );
        String jsonStr = asRolePremissionService.doAddOrUpdatePremission( roleId ,functionId , user );

        return jsonStr;
    }

    //删除角色授权
    @RequestMapping(value = "/cancelPremission" , produces="application/json;charset=UTF-8")
    @ResponseBody
    @MethodLog( operateInfo = "取消角色权限配置" )
    public String cancelPremission( Integer roleId , Integer functionId )
    {
        String jsonStr = asRolePremissionService.doCancelPremission( roleId , functionId );
        return jsonStr;
    }

    //前往角色管理页面
    @RequestMapping(value = "/toRoleManagePage")
    public String toRoleManagePage()
    {
        return "systemManage/roleManage";
    }

    //响应角色管理页面请求
    @RequestMapping(value = "/roleManage", produces="application/json;charset=UTF-8")
    @ResponseBody
    @MethodLog( operateInfo = "查看角色管理" )
    public String roleManage( Integer page , @RequestParam(value = "rows") Integer size )
    {

        String jsonStr = asRoleService.returnRoleManageDataGridJsonStr( page , size );

        return jsonStr;
    }

    //添加角色
    @RequestMapping(value = "/addRole", produces="application/json;charset=UTF-8")
    @ResponseBody
    @MethodLog( operateInfo = "添加角色" )
    public String addRole( AsRole role , HttpSession session )
    {
        AsUser user = (AsUser)session.getAttribute( "user" );
        String jsonStr = asRoleService.addRole( role , user );

        return jsonStr;
    }

    //删除角色
    @RequestMapping(value = "/delRole", produces="application/json;charset=UTF-8")
    @ResponseBody
    @MethodLog( operateInfo = "删除角色" )
    public String delRole( Integer id )
    {
        String jsonStr = asRoleService.delRole( id );

        return jsonStr;
    }

    //更新角色
    @RequestMapping(value = "/updateRole", produces="application/json;charset=UTF-8")
    @ResponseBody
    @MethodLog( operateInfo = "更新角色" )
    public String updateRole( AsRole role )
    {
        String jsonStr = asRoleService.updateRole( role );

        return jsonStr;
    }

    //查询角色
    @RequestMapping(value = "/selectRole", produces="application/json;charset=UTF-8")
    @ResponseBody
    public String selectRole( Integer id )
    {
        String jsonStr = asRoleService.returnOneRoleById( id );

        return jsonStr;
    }

    //前往关键词审核界面
    @RequestMapping(value = "/toKeywordsCheckPage")
    public String toKeywordsCheckPage()
    {
        return "systemManage/keywordsCheck";
    }

    //显示当前用户的关键词申请管理页面
    @RequestMapping( value = "/showKeywordsCheckDataGrid" , produces = "application/json;charset=UTF-8" )
    @ResponseBody
    @MethodLog( operateInfo = "查看关键词审核" )
    public String showKeywordsCheckPage(
            Integer page ,
            @RequestParam( value = "rows" ) Integer size ,
            String keywords ,
            HttpSession session )
    {
        //查询所有关键词故agentId为null
        String jsonStr = asKeywordsService.returnKeywordsDataGridJsonStr( null , keywords , page , size );

        return jsonStr;
    }

    //关键词审核页面 修改审核状态 使用状态
    @RequestMapping( value = "/modifyCheckOrUse" )
    @ResponseBody
    @MethodLog( operateInfo = "" )
    public String modifyCheckOrUse( AsKeywords keywords , String action )
    {
        String methodName = "modifyCheckOrUse";
        Class [] classes = new Class[]{ AsKeywords.class , String.class };
        String operateInfo = "";
        String jsonStr = "";
        switch( action )
        {
            //修改审核状态为审核中
            case "isChecking":
                jsonStr = asKeywordsService.setChecking( keywords );
                operateInfo = "修改关键词审核状态为审核中";
                break;
            //修改审核状态为不通过
            case "checkIsNo":
                jsonStr = asKeywordsService.setCheckIsNo( keywords );
                operateInfo = "修改关键词审核状态为不通过";
                break;
            //修改使用状态为不使用
            case "notUse":
                jsonStr = asKeywordsService.setNotUse( keywords );
                operateInfo = "修改关键词使用状态为不使用";
                break;
            //修改使用状态为不使用
            case "startUse":
                jsonStr = asKeywordsService.setStartUse( keywords );
                operateInfo = "修改关键词使用状态为使用";
                break;
        }
        AnnotationUtil.setOperateInfo( SystemConfigController.class , methodName , operateInfo , classes );
        return jsonStr;
    }

    //关键词审核页面 关键词审核通过
    @RequestMapping( value = "/keywordsIsOk" )
    @ResponseBody
    @MethodLog( operateInfo = "关键词审核通过" )
    public String keywordsIsOk( Integer id , Integer detailType , HttpSession session )
    {
        AsUser user = (AsUser)session.getAttribute( "user" );
        Integer resultCode = asKeywordsService.submitKeywordsIsOk( id , detailType , user );

        return JsonUtil.successOrNotJsonStr( resultCode );
    }

    //前往财务管理页面
    @RequestMapping(value = "/toFinancialManagePage")
    public String toFinancialManagePage()
    {
        return "systemManage/financialManage";
    }

    //财务操作
    @RequestMapping( value = "/financialOperate" , produces = "application/json;charset=UTF-8" )
    @ResponseBody
    @MethodLog( operateInfo = "财务管理" )
    public String financialOperate( AsAccountDetail detail , HttpSession session , String userName )
    {
        AsUser user = (AsUser)session.getAttribute( "user" );
        Integer resultCode = asAccountDetailService.submitFinancialOperate( detail , user , userName );

        return JsonUtil.successOrNotJsonStr( resultCode );
    }

    //前往用户管理页面
    @RequestMapping(value = "/toUserManagePage")
    public String toUserManagePage()
    {
        return "systemManage/userManage";
    }

    //显示用户管理页面
    @RequestMapping( value = "/showUserManage" , produces = "application/json;charset=UTF-8" )
    @ResponseBody
    @MethodLog( operateInfo = "用户管理" )
    public String showUserManage( Integer page , @RequestParam(value = "rows")Integer size )
    {
        String jsonStr = asUserService.returnUserManagePageJsonStr( page , size );

        return jsonStr;
    }

    //显示指定用户的日志
    @RequestMapping( value = "/showLogsOfUser"  , produces = "application/json;charset=UTF-8" )
    @ResponseBody
    @MethodLog( operateInfo = "查看用户日志" )
    public String showLogsOfUser( Integer page , @RequestParam( value = "rows") Integer size , Integer userId , String logDate  )
    {
        //获取搜索条件中的查询时间
        Date searchDate = null;
        if( logDate != null && !logDate.equals( "" ) )
        {
            //将查询时间转换为date对象
            searchDate = DateUtil.strToDate( logDate );
        }
        String jsonStr = asLogsService.returnLogsPageByUserId( userId , searchDate , page , size );

        return jsonStr;
    }

    //显示指定用户预付款页面
    @RequestMapping( value = "/showAgentPrePay"  , produces = "application/json;charset=UTF-8" )
    @ResponseBody
    @MethodLog( operateInfo = "查看用户预付款" )
    public String showAgentPrePay(
            Integer page , @RequestParam( value = "rows") Integer size ,
            String start , String end , Integer detailType , Integer userId , HttpSession session )
    {
        String jsonStr = asAccountDetailService.returnAccountDetailOfPageJsonStr( userId , page , size , start , end , detailType );

        return jsonStr;
    }

    //添加用户(用户管理页面)
    @RequestMapping( value = "/doAddUser"  , produces = "application/json;charset=UTF-8" )
    @ResponseBody
    @MethodLog( operateInfo = "添加用户" )
    public String doAddUser( AsUser user , HttpSession session )
    {
        AsUser currentUser = (AsUser)session.getAttribute( "user" );
        return  asUserService.addUser( user , currentUser.getUserName() );
    }

    //更新用户(用户管理页面)
    @RequestMapping( value = "/doUpdateUser"  , produces = "application/json;charset=UTF-8" )
    @ResponseBody
    @MethodLog( operateInfo = "更新用户" )
    public String doUpdateUser( AsUser user )
    {
        return  asUserService.updateOneUser( user );
    }

    //删除用户(用户管理页面)
    @RequestMapping( value = "/doDeleteUser"  , produces = "application/json;charset=UTF-8" )
    @ResponseBody
    @MethodLog( operateInfo = "删除用户" )
    public String doDeleteUser( Integer id )
    {
        return  asUserService.delUser( id );
    }

    //获取所有可用角色(用户管理页面)
    @RequestMapping( value = "/roleComboboxJsonStr"  , produces = "application/json;charset=UTF-8" )
    @ResponseBody
    public String roleComboboxJsonStr( )
    {
        return asRoleService.returnAllAvailableRoleJsonStr();
    }

    //根据id获取指定用户(用户管理页面)
    @RequestMapping( value = "/doGetUserById"  , produces = "application/json;charset=UTF-8" )
    @ResponseBody
    public String doGetUserById( Integer id )
    {
        return asUserService.returnOneUserJsonStr( id );
    }
}
